package views
{
	import flash.events.GeolocationEvent;
	import flash.sensors.Geolocation;
	
	import inerface.IScenicShopPanel;
	
	import main.ClientConstants;
	import main.ViewSubBase;
	
	import panel.scenicShop.ScenicShopPanel;
	
	import struct.TourElementBase;
	
	public class ScenicShopView extends ViewSubBase
	{
		private var _panel:IScenicShopPanel;
		private var geoloc:Geolocation = new Geolocation();
		
		private var currentShowList:Array;
		private var pageNum:int = 0;
		
		private var dataList:Array;
		
		public function ScenicShopView()
		{
			currentShowList = [];
		}
		
		public override function show():void
		{
			data.viewSpot.loadShopData(render);
		}
		
		protected override function render():void
		{
			if(_panel == null){
				_panel = new ScenicShopPanel();
				_panel.init(view.layout.stage,view.resourceLoader,lang,view.uri);
				_panel.onShowDetail = showDetail;
				_panel.showLocation = showLocation;
				_panel.clickTurnPage = turnPage;		
				
				dataList = data.viewSpot.shopList;
				
				currentShowList = dataList.slice(0,10 * (pageNum + 1));
				_panel.changeData(currentShowList);
				
				getLocation();
			}
			this.addView(_panel);
		}
		
		private function getLocation():void
		{
			if(Geolocation.isSupported){
				if(geoloc.muted == false){
					geoloc.addEventListener(GeolocationEvent.UPDATE,onGeoUpdataHandler);
				}
			}
		}
		
		private function showDetail(data:TourElementBase):void
		{
			view.viewSpotDetail.showData(data,ClientConstants.DETAIL_SHOP);
		}
		
		private function showLocation(obj:*):void
		{
			view.showLocation.objInfo = obj;
			view.showLocation.show();
		}
		
		protected function onGeoUpdataHandler(e:GeolocationEvent):void
		{
			geoloc.removeEventListener(GeolocationEvent.UPDATE,onGeoUpdataHandler);
			
			var lat:Number = e.latitude;
			var lon:Number = e.longitude;
			if(_panel){
				_panel.changeLocation = [lat,lon];
			}
		}
		
		private function turnPage():void
		{
			if(dataList[(pageNum + 1) * 10]){
				pageNum ++;
				
				currentShowList = dataList.slice(0,10 * (pageNum + 1));//);currentShowList.concat(
				_panel.changeData(currentShowList);
			}
		}
		
		public function clearRecord():void
		{
			pageNum = 0;
		}
		
		public override function close():void
		{
			this.closeView(this);
		}
		
		public override function clear():void
		{
			if(_panel){
//				pageNum = 0;
				currentShowList = [];
				dataList = [];
				
				_panel.clear();
				_panel = null;
				view.destroyObject(this as Class);
			}
		}
		
	}
}